package org.itboys.baseobject.service;

import java.util.List;

import javax.annotation.Resource;

import org.itboys.baseobject.entity.KeyValuesConfig;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.redis.RedisCacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

/**
 * KEY-List<String> value配置逻辑层
 * @author weisky
 *
 */
@Service
public class KeyValuesConfigService extends BaseService<KeyValuesConfig, Long>{
	
	private static final long serialVersionUID = -8598285486260559411L;
	
	@Resource(name="baseMongoDS")
	private MongoDataSource ds;
	@Autowired
	private RedisCacheClient redisCacheClient;
	
	//private String KEY_VALUES_CONFIG_CACHE = "KVALUES_CONFIG_";

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<KeyValuesConfig> getEntityClass() {
		return KeyValuesConfig.class;
	}

	/**
	 * 新增或修改
	 * @param keyValuesConfig
	 */
	public void saveOrUpdate(KeyValuesConfig keyValuesConfig){
		if(keyValuesConfig.getId() != 0){
			super.update(keyValuesConfig);
		}else{
			super.save(keyValuesConfig);
		}
	}
	
	/**
	 * 根据key值   获取values集合
	 * @param key
	 * @return
	 */
	public List<String> getValuesByKey(String key){
		List<KeyValuesConfig> list = super.findByField("key", key);
		if(!list.isEmpty()){
			List<String> slist = Lists.newArrayListWithExpectedSize(list.size());
			for(KeyValuesConfig kv : list){
				slist.add(kv.getValue());
			}
			return slist;
		}
		return null;
	}
	
	/**
	 * 根据key值  获取集合
	 * @param key
	 * @return
	 */
	public List<KeyValuesConfig> getListByKey(String key){
		return super.findByField("key", key);
	}
	
}
